package com.financing.wap.web.controller.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.PublicActivitiesDTO;
import com.financing.wap.dto.PublicActivitiesPersonDTO;
import com.financing.wap.service.IPublicActivitiesService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.PublicActivitiesVo;

/**
 * 公共活动Controller
 * @author lisu
 *
 */
@Controller("publicActivitiesController")
@RequestMapping("/api/publicActivities")
public class PublicActivitiesController {
	
	@Autowired
	private IPublicActivitiesService publicActivitiesServiceImpl;
	
	Logger logger = Logger.getLogger(getClass());
	
	/**
	 * @param 
	 * @param request
	 * @param response
	 * @param bean
	 * @return
	 */
	@RequestMapping("/view")
	@ResponseBody
	public ResultMap view(HttpServletRequest request, HttpServletResponse response,PublicActivitiesDTO bean){

		ResultMap result = new ResultMap();
		
		try {
			logger.info("In  query publicActivities info Begin id=:"+bean.getId());
			PublicActivitiesVo publicActivitiesVo = publicActivitiesServiceImpl.queryPublicActivities(bean);
			result.setAttribute("dto", publicActivitiesVo);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			logger.info("In query publicActivities end");
		}catch(Exception e) {
			logger.error("error PublicActivitiesController view:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	/**
	 * 展示所有产品列表
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, HttpServletResponse respons,Pager pager){
		ResultMap result = new ResultMap();
		Map<String,Object> parameter =  new HashMap<String,Object>();
		
		try {
			logger.info("In  query publicActivitiesList info Begin ");
			
			logger.info("In  query publicActivitiesList info pageNo="+request.getParameter("pageNo"));
			//查询活动总数
			int count = this.publicActivitiesServiceImpl.getPublicActivitiesCount(parameter);
			
			logger.info("In  query publicActivitiesList info count="+count);
			pager.setTotalCount(count);
			
			//活动总数 不小于0 查询列表数据,否则不查询
			if(count <= 0){
				result.setAttribute("list", null);
			}else{
				parameter.put("pager", pager);
				List<PublicActivitiesVo> publicActivitiesList = publicActivitiesServiceImpl.getPublicActivitiesList(parameter);
				for (PublicActivitiesVo publicActivitiesVo : publicActivitiesList) {
					//列表数据  将内容为NULL
					publicActivitiesVo.setActivitiesRecomment(null);
				}
				result.setAttribute("list", publicActivitiesList);
			}
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("pager", pager);
			logger.info("In  query publicActivitiesList info end");
		} catch (Exception e) {
			logger.error("error when publicActivities product list:",e);
			result.setStatus(ResultMap.STATUS.WARN);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setMessage("查询公共活动列表异常!");
			return result;
		}
		
		return result;
	}
	@RequestMapping( value="/addParticipatePerson")
	@ResponseBody
	public ResultMap addParticipatePerson(HttpServletRequest request, HttpServletResponse response,PublicActivitiesPersonDTO bean){

		ResultMap result = new ResultMap();
		
		try {
			logger.info("In add ParticipatePerson Begin ");
			
			if(bean.getPublicActivitiesId() ==null || StringUtil.isNullOrEmptyStr(bean.getPersonMobile()) || StringUtil.isNullOrEmptyStr(bean.getPersonName()) ){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setMessage("参数缺失");
				return result;
				
			}
			PublicActivitiesDTO publicActivitiesDTO = new PublicActivitiesDTO();
			publicActivitiesDTO.setId(bean.getPublicActivitiesId());
			PublicActivitiesVo publicActivitiesVo = publicActivitiesServiceImpl.queryPublicActivities(publicActivitiesDTO);
			if(publicActivitiesVo == null){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OBJECT_NOFOUNT);
				result.setMessage("活动不存在");
				return result;
			}
			if(!DateUtil.nowBeforeStartTime(publicActivitiesVo.getActivitiesRegistEndTime())){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_ACTIVITIES_REGISTTIME_END);
				result.setMessage("活动报名时间已经结束");
				return result;
			}
			Integer activitiesTotalPerson = publicActivitiesVo.getActivitiesTotalPerson();
			Integer activitiesParticipatePerson = publicActivitiesVo.getActivitiesParticipatePerson();
			if(activitiesTotalPerson != null && ( activitiesParticipatePerson == null || activitiesTotalPerson <= activitiesParticipatePerson)){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_JOIN_PERSON_FULL);
				result.setMessage("活动参与人数已满");
				return result;
			}
			//初始化bean属性
			DTOdateSetUtil.setCreateInfo(bean);
			DTOdateSetUtil.setUpdateInfo(bean);
			publicActivitiesServiceImpl.addPublicActivitiesPerson(bean);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("添加参与人成功");
			logger.info("In add ParticipatePerson end");
		}catch(Exception e) {
			logger.error("error PublicActivitiesController addParticipatePerson:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setMessage("添加参与人失败");
		}
		return result;
	} 
}

